<template>
   <div>
      <v-input :detailData="inputData"
               :inputConfig="inputConfig"
               :rules="rules"
               @keyEnter="keyEnter"
               ref="input"></v-input>
      <div class="inputRecord">
         {{ scansku }}
      </div>
   </div>

</template>

<script>
import input from "@/components/commons/Detail";

export default {
   name: "",
   data() {
      return {
         inputData: {
            receiptkey: "",
            suppliername: "",
            receiptlpn: "",
            sku: "",
            qty: 0,
            totalqty: ""
         },
         inputConfig: [
            [
               {
                  type: "input",
                  label: this.$t('in.asnCode'),
                  props: "receiptkey",
                  disabled: true
               },
               {
                  type: "input",
                  label: this.$t('config.supplier'),
                  props: "suppliername",
                  disabled: true
               },
               {
                  type: "input",
                  label: this.$t('in.standardlpn'),
                  props: "receiptlpn",
               },
            ],
            [
               {
                  type: "input",
                  label: this.$t('config.sku'),
                  props: "sku",
                  width: "280px",
                  height: "50px",
               },
               {
                  type: "input",
                  label: this.$t('out.qty'),
                  props: "qty",
                  disabled: true
               },
               {
                  type: "input",
                  label: this.$t('out.totalqty'),
                  props: "totalqty",
                  disabled: true
               },
            ],
         ],
         rules: {},
         scansku: ""
      }
   },
   methods: {
      keyEnter(name, data) {
         if (name === "receiptlpn") {
            let receiptlpn = this.$trim(data).toUpperCase();
            if (receiptlpn === "") {
              this.$message.error(this.$tj('in.standardlpn','app.emptyMes'));
               this.errorPlay();
               return;
            }
            this.checkReceiptlpn(receiptlpn);
         } else if (name === "sku") {
            let receiptlpn = this.$trim(this.inputData.receiptlpn).toUpperCase();
            if (receiptlpn === "") {
              this.$message.error(this.$tj('in.standardlpn','app.emptyMes'));
               this.errorPlay();
               return;
            }
            let sku = this.$trim(data).toUpperCase();
            if (sku === "") {
              this.$message.error(this.$tj('config.sku','app.emptyMes'));
               this.errorPlay();
               return;
            }
            this.scansku = sku;
            if (sku === "END") {
               this.$parent.$refs.btn.confirm();
            } else {
               this.checkSku(sku);
            }
         }
      },
      async checkReceiptlpn(data) {
         try {
            let res = await this.$api.post(`/ReceiptReversalCheckReceiptlpn`, {
               whseid: this.$parent.whseid,
               opeuser: this.$parent.opeuser,
               users: this.$parent.users,
               receiptlpn: data,
               receiptkey: this.inputData.receiptkey
            })
            if (res.data.type === "error") {
               this.$message.error(res.data.msg);
               this.$refs.input.inputFocus("receiptlpn");
               this.errorPlay();
            } else if (res.data.type === "success") {
               this.inputData.qty = res.data.qty;
               this.inputData.totalqty = res.data.totalqty;
               this.$parent.$refs.table.tableData = res.data.jsonList;
               this.$refs.input.inputFocus("sku");
            }
         } catch (e) {
            this.$message.error(this.$t('app.err'));
            this.errorPlay();
         }
      },
      async checkSku(data) {
         try {
            let res = this.$api.post(`/ReceiptReversalScanSku`, {
               whseid: this.$parent.whseid,
               opeuser: this.$parent.opeuser,
               users: this.$parent.users,
               receiptkey: this.$trim(this.inputData.receiptkey),
               receiptlpn: this.$trim(this.inputData.receiptlpn),
               sku: data,
            })
            if (res.data.type === "error") {
               this.$message.error(res.data.msg);
               this.$refs.input.inputFocus("sku");
               this.errorPlay();
            } else {
               this.inputData.qty = res.data.qty;
               this.inputData.totalqty = res.data.totalqty;
               this.$parent.$refs.table.tableData = res.data.jsonList;
               this.$refs.input.inputFocus("sku");
            }
         } catch (e) {
            this.$message.error(this.$t('app.err'));
            this.$refs.input.inputFocus("sku");
            this.errorPlay();
         }
      }
   },
   mounted() {

   },
   components: {
      "v-input": input,
   }
}
</script>

<style scoped>

</style>
